import Vue from 'vue';
import Axios from 'axios';
import Qs from 'qs';
import VModal from 'vue-js-modal';
import '../basic/basic.css';
import paging from '../components/pageing/paging.js';
Vue.use(VModal,{dialog:true});

new Vue({
    el:'#louie-list',
    data:{
        //--公共--
        url:$('#louie-list').attr('data-url'),
        //--列表--
        list:[],
        total:0,  //总数
        returnNum:10,  //请求数据数量
        pageNum:1,  //当前page页面
    },
    components:{
        paging:paging
    },
    created:function(){
        this.getTotal();
        this.li();
    },
    methods:{
        //获取total
        getTotal:function(){
            Axios.post(this.url,Qs.stringify({
                pages:'treasure',
                doing:'total'
            }))
                .then((res)=>{
                    this.total = parseInt(res.data.data);
                })
                .catch(error=>console.log(error));
        },
        //获取列表
        li:function(){
            Axios.post(this.url,Qs.stringify({
                pages:'treasure',
                doing:'li',
                limit:[(this.pageNum-1)*this.returnNum,this.returnNum]
            }))
                .then((res)=>{
                    var data = res.data;
                    if(data.status == 'success'){
                        this.list = data.data;
                    }else{
                        this.list = [];
                    }
                })
                .catch(error=>console.log(error));
        },
        //删除数据
        delData:function(treasure_id){
            var self = this;
            this.confirm('确认要删除该宝藏吗？',function(is_true){
                if(is_true){
                    Axios.post(self.url,Qs.stringify({
                        pages:'treasure',
                        doing:'delete',
                        treasure_id:treasure_id
                    }))
                        .then((res)=>{
                            if(res.data.status == 'success'){
                                self.getTotal();
                                self.li();
                            }else{
                                self.alert('删除失败！');
                            }
                        })
                        .catch(error=>console.log(error));
                }
            });
        },
        //切换分页
        changePage:function(page){
            this.pageNum = page;
            this.li();
        },
        //alert
        alert:function(text){
            this.$modal.show('dialog',{
                text:text,
                buttons:[
                    {title:'关闭'}
                ]
            });
        },
        //confirm
        confirm:function(text,call){
            var self = this;
            this.$modal.show('dialog',{
                text:text,
                buttons:[
                    {
                        title: '取消',
                        handler:function(){
                            self.$modal.hide('dialog');
                            call(false);
                        }
                    },
                    {
                        title: '确认',
                        default: true,
                        handler:function(){
                            self.$modal.hide('dialog');
                            call(true);
                        }
                    }
                ]
            });
        }
    }
});

